import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { getChangeMobileCode, setUserStatus } from './api';
import { airportList } from '@apps/ant-bus/src/views/business/airport/data/api';
import { weChatList } from '@apps/ant-bus/src/views/business/platformManage/wechat/data/api';
import { RenderType } from '@pkg/setting';

export const columns: BasicColumn[] = [
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 150,
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 80,
    renderType: RenderType.AVATAR,
  },
  {
    title: '登录账号',
    dataIndex: 'mainAccount',
    width: 100,
  },
  {
    title: '姓名',
    dataIndex: 'userName',
    width: 150,
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
    width: 120,
  },
  {
    title: '应急手机号',
    dataIndex: 'telephone',
    width: 120,
  },
  {
    title: '会员类型',
    dataIndex: 'isOld',
    width: 90,
    dictData: [
      { label: '有卡会员', value: true, color: '#09d550' },
      { label: '无卡会员', value: false, color: '#a6a9a6' },
    ],
    renderType: RenderType.DICT,
  },
  {
    title: '性别',
    dataIndex: 'sex',
    width: 60,
    dictData: [
      { label: '女', value: 1 },
      { label: '男', value: 2 },
    ],
    renderType: RenderType.DICT,
  },
  {
    title: '年龄',
    dataIndex: 'age',
    width: 60,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange() {
          record.pendingStatus = false;
          setUserStatus(record.userId)
            .then(data => {
              record.status = data;
            })
            .catch(data => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '所属公众号',
    dataIndex: 'weChatName',
    width: 120,
  },
  {
    title: '注册方式',
    dataIndex: 'registerWayLabel',
    width: 120,
  },
  {
    title: '注册地',
    dataIndex: 'airportName',
    width: 150,
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: 180,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'account',
    label: '登录账号',
    component: 'Input',
    colProps: { span: 4 },
  },
  {
    field: 'userName',
    label: '姓名',
    component: 'Input',
    colProps: { span: 4 },
  },
  {
    field: 'userMobile',
    label: '手机号',
    component: 'Input',
    colProps: { span: 4 },
  },
  {
    field: 'airportId',
    label: '注册地',
    component: 'ApiSelect',
    componentProps: {
      api: airportList,
      params: { handlerName: 'busAirportRoleListHandler' },
      labelField: 'airportName',
      valueField: 'airportId',
      showSearch: true,
      immediate: true,
    },
  },
  {
    field: 'beginDate',
    label: '开始日期',
    component: 'DatePicker',
    colProps: { span: 5 },
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
      format: 'YYYY-MM-DD',
    },
  },
  {
    field: 'endDate',
    label: '结束日期',
    component: 'DatePicker',
    colProps: { span: 5 },
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
      format: 'YYYY-MM-DD',
    },
  },
];

export const editUserFormSchema: FormSchema[] = [
  {
    field: 'userId',
    label: '用户Id',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    field: 'userName',
    label: '姓名',
    component: 'Input',
    required: false,
  },
  {
    field: 'mobile',
    label: '会员手机号',
    component: 'Input',
    required: false,
  },
  {
    field: 'telephone',
    label: '应急电话',
    component: 'Input',
    required: false,
  },
  {
    field: 'sex',
    label: '性别',
    component: 'Select',
    required: false,
    componentProps: {
      options: [
        { label: '女', value: 1 },
        { label: '男', value: 2 },
      ],
    },
  },
  {
    field: 'age',
    label: '年龄',
    component: 'InputNumber',
    required: false,
  },
  {
    field: 'weChatId',
    label: '所属公众号',
    component: 'ApiSelect',
    componentProps: {
      api: weChatList,
      labelField: 'weChatName',
      valueField: 'weChatId',
      showSearch: true,
      immediate: true,
    },
  },
  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    required: false,
  },
];

export const changeMobileFormSchema: FormSchema[] = [
  {
    field: 'userId',
    label: '用户Id',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '选择换绑证明方式',
    field: 'modeType',
    component: 'RadioGroup',
    required: true,
    defaultValue: 2,
    dynamicDisabled: ({ values }) => {
      const regex = /^1[3-9]\d{9}$/;
      let isMobile = regex.test(values.mainAccount);
      if (isMobile) {
        return false;
      } else {
        return true;
      }
    },
    componentProps: {
      options: [
        { label: '原手机号验证码校验', value: 1 },
        { label: '上传申请函', value: 2 },
      ],
    },
  },
  {
    field: 'mainAccount',
    component: 'Input',
    label: '会员原账号',
    required: true,
    dynamicDisabled: true,
    ifShow: ({ model }) => {
      if (model.modeType == 1) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    label: '申请函证明',
    field: 'attachImg',
    component: 'ImageUpload',
    required: true,
    ifShow: ({ model }) => {
      if (model.modeType == 2) {
        return true;
      } else {
        return false;
      }
    },
  },

  {
    field: 'weChatId',
    label: '所属公众号',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: weChatList,
      labelField: 'weChatName',
      valueField: 'weChatId',
      showSearch: false,
      immediate: true,
    },
    ifShow: ({ model }) => {
      if (model.modeType == 1) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    field: 'changeMobileCode',
    component: 'InputCountNewDown',
    label: '验证码',
    required: true,
    componentProps: ({ formModel }) => {
      let params = { userId: formModel.userId, weChatId: formModel.weChatId };
      return {
        count: 120,
        sendCodeApi: getChangeMobileCode,
        params: params,
      };
    },
    ifShow: ({ model }) => {
      if (model.modeType == 1) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    field: 'mobile',
    label: '会员新手机账号',
    component: 'Input',
    rules: [
      { required: true, message: '请输入手机号' },
      { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式错误！' },
    ],
  },
];

export const importFormSchema: FormSchema[] = [
  {
    label: 'excel',
    component: 'FileUpload',
    required: false,
  },
];
